#!/bin/bash
version=5.7.43
package_name=mysql-${version}-linux-glibc2.12-x86_64

yum remove `rpm -qa| grep mariadb` -y
yum install wget libaio -y
wget http://aliyun-ops.oss-cn-beijing.aliyuncs.com/software/${package_name}.tar.gz
tar -xf ${package_name}.tar.gz -C /usr/local/
cd /usr/local/ && ln -sv ${package_name} mysql
useradd -s /sbin/nologin mysql
chown -R mysql.mysql mysql && chown -R mysql.mysql ${package_name}
echo -e 'export MYSQL_HOME=/usr/local/mysql\nexport PATH=$PATH:$MYSQL_HOME/bin' >>  /etc/profile.d/mysql.sh
mkdir -p /data/mysql/data
chown -R mysql.mysql /data/mysql/data
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/loca/mysql --datadir=/data/mysql/data
cat >/etc/my.cnf <<EOF
[mysqld]
skip_ssl
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
symbolic-links=0
#!includedir /etc/my.cnf.d
init_connect='SET NAMES utf8'
skip-character-set-client-handshake
max_allowed_packet = 34M
innodb_log_file_size = 256M
transaction-isolation=READ-COMMITTED
default-storage-engine=INNODB
character_set_server=utf8mb4
innodb_default_row_format=DYNAMIC
innodb_large_prefix=ON
innodb_file_format=Barracuda
innodb_log_file_size=2G
EOF

chown -R mysql.mysql /etc/my.cnf

cat >/etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
EOF

systemctl start mysqld && systemctl enable mysqld
#/usr/local/mysql/bin/mysqladmin -hlocalhost -uroot password CloudEasy@2020
#/usr/local/mysql/bin/mysql -uroot -pCloudEasy@2020 -NBe "update mysql.user set host = '%' where user = 'root';"
#/usr/local/mysql/bin/mysql -uroot -pCloudEasy@2020 -NBe "flush privileges;"
#reboot


cat > /tmp/init.sh <<EOF
#!/bin/bash
mysqladmin -hlocalhost -uroot password CloudEasy@2020
mysql -uroot -pCloudEasy@2020 -NBe "update mysql.user set host = '%' where user = 'root';"
mysql -uroot -pCloudEasy@2020 -NBe "flush privileges;"
echo -e "\033[31mROOT密码：\033[33mCloudEasy@2020\033[0m"
EOF

echo -e "\033[31m请按顺序执行：\n\033[31m第一步：\033[32msource /etc/profile.d/mysql.sh\n\033[31m第二步：\033[32msh /tmp/init.sh\n\033[31m提醒：\033[31m若执行报错，从第一步重新执行\033[0m"
